﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SourceRestore
{
    public partial class OptionsWindow : Form
    {
        public OptionsWindow()
        {
            InitializeComponent();
        }

        private void OptionsWindow_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.IgnoreFileTypes == null)
                Properties.Settings.Default.IgnoreFileTypes = new System.Collections.Specialized.StringCollection();
            lstExclusions.Items.AddRange((from string s in Properties.Settings.Default.IgnoreFileTypes
                                          orderby s
                                          select s).ToArray());
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (txtFileType.Text.Length > 0)
            {
                if (lstExclusions.SelectedItem != null)
                {
                    lstExclusions.Items[lstExclusions.SelectedIndex] = txtFileType.Text;
                }
                else
                {
                    if ((from object entry in lstExclusions.Items
                         where ((string)entry).ToUpper() == txtFileType.Text.ToUpper()
                         select entry).Count() > 0)
                    {
                        MessageBox.Show("An entry with this value already exist!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    lstExclusions.Items.Add(txtFileType.Text);
                    lstExclusions.SelectedItem = txtFileType.Text;
                }
                cmdNew_Click(null, null);
            }
        }

        private void cmdNew_Click(object sender, EventArgs e)
        {
            lstExclusions.SelectedIndex = -1;
            cmdDelete.Enabled = false;
            txtFileType.Text = "";
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (lstExclusions.SelectedItem != null)
            {
                if (MessageBox.Show("Are you sure you want to remove this file extension?", "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    lstExclusions.Items.Remove(lstExclusions.SelectedItem);
                    txtFileType.Text = "";
                    cmdDelete.Enabled = false;
                }
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.IgnoreFileTypes = new System.Collections.Specialized.StringCollection();
            Properties.Settings.Default.IgnoreFileTypes.AddRange((from string s in lstExclusions.Items
                                                                  select s).ToArray());
            DialogResult = DialogResult.OK;
            Close();
        }

        private void lstExclusions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstExclusions.SelectedItem != null)
            {
                string filter = (string)lstExclusions.SelectedItem;
                txtFileType.Text = filter;
                cmdDelete.Enabled = true;
            }
        }
    }
}
